/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmViewApts.java
 *
 * Created on Mar 19, 2010, 9:06:01 PM
 */

package forms;

import java.awt.GridLayout;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import rad.Java.MotelPlus.List.AptTypes;
import rad.Java.MotelPlus.List.Apts;
import rad.Java.MotelPlus.List.Blocks;
import rad.Java.MotelPlus.Views.AptView;
import rad.Java.MotelPlus.Class.Setting;
/**
 *
 * @author rogerio
 */
public class frmViewApts extends javax.swing.JFrame {

    // <editor-fold defaultstate="collapsed" desc="Declarations of variables">
        private Vector<AptView> btns;
        private static final long serialVersionUID = 1L;
        private Apts listapt = new Apts();
        private Blocks listblocs = new Blocks();
        private AptTypes listTypes = new AptTypes();
        private GridLayout l;
        private Setting setting = new Setting();
        private int rows, columns;
    // </editor-fold>
    /** Creates new form frmViewApts */
    public frmViewApts() {
        initComponents();
        setSize(800, 600);
        btns = new Vector<AptView>();
        fillBlocks();
        listTypes.fillList();

        if (!setting.readSettings(setting.getFileName()))
        {
            JOptionPane.showMessageDialog(this, "Erro ao ler configurações:\n"+setting.getErrorMessage(),"Erro",JOptionPane.ERROR_MESSAGE);
        }
        else
        {
            try
            {
                rows = Integer.parseInt(setting.getRowsNumber());
                columns = Integer.parseInt(setting.getColsNumber());
            }
            catch(Exception e)
            {
                rows = 5;
                columns = 5;
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="Functions">
        private void createApts(String id)
        {
            listapt.fillListByBlock(id);
            int tam = listapt.count();
            l = new GridLayout(rows, columns, 5, 5);
            JPanel pnlEsquerdo = new JPanel(l);
            for (int i = 0; i < tam; i++) {
                AptView tmp = new AptView(listapt.get(i));
                btns.add(tmp);
                btns.get(i).setEstado(listapt.get(i).getStatus());
                listTypes.search(listapt.get(i).getTypeId());
                btns.get(i).btnName(listapt.get(i).getNumber()+"-"+listTypes.get().getShortName());
                pnlEsquerdo.add(btns.get(i));
            }
            jScrollPane1.setViewportView(pnlEsquerdo);
        }

        private void fillBlocks()
        {
            listblocs.fillList();
            for(int i = 0; i < listblocs.count(); i++)
            {
               listBlock.add(listblocs.get(i).getName());
            }
        }
    // </editor-fold>

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlAux = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        listBlock = new java.awt.List();
        pnlApts = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setLocationByPlatform(true);
        setName("Form"); // NOI18N

        pnlAux.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        pnlAux.setName("pnlAux"); // NOI18N
        pnlAux.setPreferredSize(new java.awt.Dimension(200, 633));

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(motelplus.MotelPlusApp.class).getContext().getResourceMap(frmViewApts.class);
        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        listBlock.setFont(resourceMap.getFont("listBlock.font")); // NOI18N
        listBlock.setName("listBlock"); // NOI18N
        listBlock.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                listBlockMouseClicked(evt);
            }
        });
        listBlock.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                listBlockActionPerformed(evt);
            }
        });
        jScrollPane2.setViewportView(listBlock);

        javax.swing.GroupLayout pnlAuxLayout = new javax.swing.GroupLayout(pnlAux);
        pnlAux.setLayout(pnlAuxLayout);
        pnlAuxLayout.setHorizontalGroup(
            pnlAuxLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlAuxLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlAuxLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(13, Short.MAX_VALUE))
        );
        pnlAuxLayout.setVerticalGroup(
            pnlAuxLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlAuxLayout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(196, Short.MAX_VALUE))
        );

        getContentPane().add(pnlAux, java.awt.BorderLayout.EAST);

        pnlApts.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        pnlApts.setName("pnlApts"); // NOI18N
        pnlApts.setPreferredSize(new java.awt.Dimension(0, 0));
        pnlApts.setLayout(new java.awt.GridLayout(1, 0));

        jScrollPane1.setName("jScrollPane1"); // NOI18N
        pnlApts.add(jScrollPane1);

        getContentPane().add(pnlApts, java.awt.BorderLayout.CENTER);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-895)/2, (screenSize.height-512)/2, 895, 512);
    }// </editor-fold>//GEN-END:initComponents

    private void listBlockActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_listBlockActionPerformed
        //String id = listblocs.get(listBlock.getSelectedIndex()).getId();
        //createApts(id);
    }//GEN-LAST:event_listBlockActionPerformed

    private void listBlockMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listBlockMouseClicked
        if(listBlock.getSelectedIndex() < 0){
            JOptionPane.showMessageDialog(rootPane, "Selecione um bloco por favor");
            return;
        }
        String id = listblocs.get(listBlock.getSelectedIndex()).getId();
        createApts(id);
    }//GEN-LAST:event_listBlockMouseClicked

    /**
    * @param args the command line arguments
    */
    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private java.awt.List listBlock;
    private javax.swing.JPanel pnlApts;
    private javax.swing.JPanel pnlAux;
    // End of variables declaration//GEN-END:variables

}
